﻿using System;
using System.Collections.Generic;
namespace XiaoSe.Algorithm
{
    /// <summary>
    /// 排序帮助类
    /// </summary>
    public class SortHelper
    {
        /// <summary>
        /// 冒泡排序
        /// </summary>
        /// <typeparam name="T">泛型类，需实现IComparable接口</typeparam>
        /// <param name="array">需要排序的数组</param>
        public void BubbleSort<T>(ref T[] array) where T : IComparable
        {
            var length = array.Length;
            for (int i = 0; i <= length - 2; i++)
            {
                for (int j = length - 1; j > i; j--)
                {
                    if (array[j].CompareTo(array[j - 1]) >= 0) continue;
                    var temp = array[j];
                    array[j] = array[j - 1];
                    array[j - 1] = temp;
                }
            }
        }

        /// <summary>
        /// 冒泡排序
        /// </summary>
        /// <typeparam name="T">泛型类，需实现IComparable接口</typeparam>
        public void BubbleSort<T>(ref List<T> list) where T : IComparable
        {
            var length = list.Count;
            for (var i = 0; i <= length - 2; i++)
            {
                for (var j = length - 1; j > i; j--)
                {
                    if (list[j].CompareTo(list[j - 1]) >= 0) continue;
                    var temp = list[j];
                    list[j] = list[j - 1];
                    list[j - 1] = temp;
                }
            }
        }
    }
}
